package com.keta.foundation.service.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.OrganizationDAO;
import com.keta.foundation.entity.Module;
import com.keta.foundation.entity.Organization;
import com.keta.foundation.entity.User;
import com.keta.foundation.exception.ExistedException;
import com.keta.foundation.exception.NotDeletedException;
import com.keta.foundation.exception.NotExistedException;
import com.keta.foundation.service.OrganizationService;
import com.keta.foundation.service.UserService;
import com.keta.foundation.util.dwz.AjaxObject;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class OrganizationServiceImpl implements OrganizationService {

	@Autowired
	private OrganizationDAO organizationDAO;

	@Autowired
	private UserService userService;

	@Override
	public List<Organization> getByParentId(Long parentId, Page page) {

		org.springframework.data.domain.Page<Organization> springPage = organizationDAO.findByParentId(parentId,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();

	}

	@Override
	public void saveOrUpdate(Organization organization) {

		Organization parentOrg = get(organization.getParent().getId());

		if (parentOrg == null) {
			throw new NotExistedException("没有上级部门!");
		}

		// 同一个部门下面不能存在相同名称的部门
		for (Organization org : parentOrg.getChildren()) {
			if (org.getName().equals(organization.getName()) && !org.getId().equals(organization.getId())) {
				throw new ExistedException(parentOrg.getName() + "下已经存在部门" + organization.getName());
			}
		}

		organizationDAO.save(organization);

	}

	@Override
	public Organization get(Long id) {

		return organizationDAO.findOne(id);
	}

	@Override
	public List<Organization> getAll() {
		return organizationDAO.findAll();
	}

	@Override
	public List<Organization> getAll(Specification<Organization> specification, Page page) {
		org.springframework.data.domain.Page<Organization> springPage = organizationDAO.findAll(specification,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public void delete(Long id) {
		Organization org = get(id);
		if (org.getChildren().size() > 0) {
			throw new NotDeletedException(org.getName() + "存在下级部门，不能删除");
		}
		List<User> users = userService.getByOrganizationId(org.getId());
		if (users.size() > 0) {
			throw new NotDeletedException(org.getName() + "下存在用户，不能删除");
		}
		organizationDAO.delete(id);
	}

	@Override
	public List<Organization> getRoot() {
		return organizationDAO.findByParentId(null);
	}

	@Override
	public List<Organization> getByParentId(Long parentId) {
		return organizationDAO.findByParentId(parentId);
	}

}
